<?php
class DateFormHelper
{

	function create($value, $formname, $textsize = 15, $maxlength=15, $addtimeselect=true)
	{
		$value = intval($value);
		$value = ($value > 0) ? $value : 0;
		$datetime = getDate($value);
		$formContents = DateFormJsCalendar::create($value, $formname.'_date', $textsize, $maxlength, $attr);
		
		if (!$addtimeselect)	return $formContents ;
		
		$minute_interval = 30 ; 
		$timearray = array();
		for ($i = 0; $i < 24; $i++) {
			for ($j = 0; $j < 60; $j = $j + $minute_interval) {
				$key = ($i * 3600) + ($j * 60);
				$timearray[$key] = ($j != 0) ? $i.':'.$j : $i.':0'.$j;
			}
		}
		ksort($timearray);
		$selectContents = sprintf('<select name="%s" id="%s" %s>'."\n", $formname.'_time', $formname.'_time', $attr);
		$selectvalue = $datetime['hours'] * 3600 + 600 * ceil($datetime['minutes'] / 10) ;
		foreach ( $timearray as $var => $label ) {
			$selected = ($var<=$selectvalue && $selectvalue<$var+$minute_interval) ? ' selected="selected"' : '' ;
			$selectContents .= sprintf("<option value='%d'%s>%s</option>\n", $var, $selected, $label);
		}
		$selectContents .= "</select>\n";
		
		return $formContents.'&nbsp;'.$selectContents ;
	}
}

class DateFormJsCalendar
{
	function create($value, $formname, $textsize=15, $maxlength=15, $attr='')
	{
		$jstime = $value; // for below javascript calendar
		ob_start();
		include_once dirname(__FILE__).'/calendarjs.php';
		$contents = ob_get_contents();
		ob_end_clean();
		
		return "$contents <input type='text' name='".$formname."' id='".$formname."' size='".$textsize."' maxlength='".$maxlength."' value='".date("Y-m-d", $value)."' ".$attr." /><input type='reset' value='Day' onclick='return showCalendar(\"".$formname."\");'><script>calDateVar=$('".$formname."');if(calDateVar.value=='1970-01-01'){calDateVar.value='';} </script>";
	}
}
?>